package org.darcy.admin.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * Druid配置组件
 */
@Configuration
public class DruidConfig {

	@Bean
	public ServletRegistrationBean<StatViewServlet> druidServlet() { // 主要实现WEB监控的配置处理
		ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<StatViewServlet>(
				new StatViewServlet(), "/druid/*"); // 进行druid监控的配置处理操作
		servletRegistrationBean.addInitParameter("loginUsername", "kf"); // 用户名
		servletRegistrationBean.addInitParameter("loginPassword", "654321"); // 密码
		servletRegistrationBean.addInitParameter("resetEnable", "true"); // 是否可以重置数据源
		return servletRegistrationBean;
	}

	@Bean
	public FilterRegistrationBean<WebStatFilter> filterRegistrationBean() {
		FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<WebStatFilter>();
		filterRegistrationBean.setFilter(new WebStatFilter());

		filterRegistrationBean.addUrlPatterns("/*"); // 所有请求进行监控处理
		// 不必监控的请求
		filterRegistrationBean.addInitParameter("exclusions", "*.html,*.png,*.ico,*.js,*.gif,*.jpg,*.css,/druid/*");
		return filterRegistrationBean;
	}

	@Bean("dataSource")
	@ConfigurationProperties(prefix = "spring.datasource")
	public DataSource druidDataSource() {
		return new DruidDataSource();
	}
}
